/*
 * @Author: redman9
 * @Date: 2018-10-22 15:24:39
 * @Last Modified by: along
 * @Last Modified time: 2020-06-15
 */
<template>
    <div class="container user-list-container">
        <div class="content-wrapper">
            <el-row
                class="search-wrapper"
                :gutter="20">
                <el-form
                    label-position="left"
                    label-width="84px"
                    size="small">
                    <!-- <el-col
                        class="filter-item"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-form-item label="选择城市：">
                            <el-select
                                v-model="filterData.city_id"
                                filterable
                                @change="fnCityChange"
                                placeholder="请选择">
                                <el-option
                                    v-for="item in cityList"
                                    :key="item.city_id"
                                    :label="item.name"
                                    :value="item.city_id"/>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="filter-item"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-form-item label="选择学校：">
                            <el-select
                                v-model="filterData.school_id"
                                filterable
                                @change="fnSchoolChange"
                                placeholder="请选择">
                                <el-option
                                    v-for="item in schoolList"
                                    :key="item.school_id"
                                    :label="item.name"
                                    :value="item.school_id"/>
                            </el-select>
                        </el-form-item>
                    </el-col> -->
                    <el-col
                        class="filter-item"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-form-item label="用户编号：">
                            <el-input
                                class="filter-tools"
                                v-model="filterData.student_no"
                                clearable
                                suffix-icon="el-icon-search"
                                @change="getTableData(1)"
                                @keyup.native.enter="getTableData(1)"
                                placeholder="请输入用户编号"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="filter-item"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-form-item label="用户昵称：">
                            <el-input
                                class="filter-tools"
                                v-model="filterData.true_name"
                                placeholder="请输入用户昵称"
                                clearable
                                suffix-icon="el-icon-search"
                                @change="getTableData(1)"
                                @keyup.native.enter="getTableData(1)"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="filter-item"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-form-item label="手机号：">
                            <el-input
                                class="filter-tools"
                                v-model="filterData.phone"
                                placeholder="请输入手机号"
                                clearable
                                suffix-icon="el-icon-search"
                                @change="getTableData(1)"
                                @keyup.native.enter="getTableData(1)"
                            />
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="filter-item"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-form-item label="任务名称：">
                            <el-select
                                v-model="filterData.task_title"
                                filterable
                                clearable
                                @change="getTableData(1)"
                                class="filter-tools"
                                placeholder="请选择"
                            >
                                <el-option
                                    v-for="item in tasks"
                                    :key="item.task_no"
                                    :label="item.title"
                                    :value="item.title" />
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col
                        class="search-btn-wrapper"
                        :md="{ span: 6 }"
                        :sm="{ span: 12 }">
                        <el-button
                            size="small"
                            type="default"
                            @click="fnReset">重置</el-button>
                        <el-button
                            size="small"
                            type="primary"
                            @click="getTableData">搜索</el-button>
                    </el-col>
                </el-form>
            </el-row>
            <div class="table-wrapper">
                <el-table
                    :data="tableData"
                    height="100%"
                    style="width: 100%;"
                    @sort-change="handleSortChanged"
                >
                    <el-table-column
                        fixed="left"
                        header-align="left"
                        align="center"
                        :width="50"
                        label="序号">
                        <template slot-scope="scope">
                            <div class="table-index">
                                {{ scope.$index + 1 }}
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column
                        fixed="left"
                        prop="student_no"
                        min-width="150"
                        align="center"
                        label="用户编号"/>
                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="true_name"
                        show-overflow-tooltip
                        :width="150"
                        align="center"
                        label="用户昵称" />
                    <el-table-column
                        prop="school_name"
                        :formatter="emptyFormatter"
                        align="center"
                        :width="150"
                        show-overflow-tooltip
                        label="学校名称"/>

                    <el-table-column
                        :formatter="emptyFormatter"
                        prop="phone"
                        min-width="110"
                        align="center"
                        label="手机号" />
                    <el-table-column
                        prop="create_time"
                        min-width="140"
                        align="center"
                        show-overflow-tooltip
                        label="注册时间">
                        <template slot-scope="scope">
                            <template v-if="scope.row.create_time">{{ scope.row.create_time*1000 | dateFormatter('yyyy/MM/dd HH:mm') }}</template>
                            <template v-else>--</template>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="taskonline_accept_count"
                        align="center"
                        sortable="custom"
                        label="远程任务参与数"
                        :width="150"
                        :formatter="emptyFormatter"
                    />
                    <el-table-column
                        prop="taskonline_verify_pass_count"
                        :formatter="emptyFormatter"
                        align="center"
                        sortable="custom"
                        label="远程任务完成数"
                        :width="150"
                    />
                    <el-table-column
                        prop="taskonline_award_money"
                        :formatter="emptyFormatter"
                        align="center"
                        sortable="custom"
                        label="已获总金额"
                        :width="120"
                    />
                </el-table>
            </div>
            <div class="pagination-wrapper clearfix">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :total="page.total_count"
                    :page-size="page.page_size"
                    @current-change="getTableData" />
            </div>
        </div>
    </div>
</template>

<script>
let sortMap = {
    'ascending': 'asc',
    'descending': 'desc'
};

export default {
    data() {
        return {
            levelTotalCount: [],
            showTotalCountFlag: false,
            authResult: {},
            tasks: [],
            tableData: [],
            filterData: {
                student_no: '',
                true_name: '',
                phone: '',
                task_title: '',
                city_id: '',
                school_id: '',
                academy_no: '',
                grade: '',
                class_no: '',
                grade_type: '',
                grade_commit: '' //提交参数
            },
            sortData: {
                sort_field: 0,
                sorttype: 1
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            cityList:[{
                city_id: '',
                name: '全部'
            }],
            schoolList: [{
                school_id: '',
                name: '全部'
            }],
            academyList: [{
                academy_no: '',
                name: '全部'
            }],
            classList:[{
                class_no: '',
                name: '全部'
            }],
            gradeList:[{
                grade: '',
                label: '全部'
            },{
                grade: 1,
                label: '大一'
            },{
                grade: 2,
                label: '大二'
            },{
                grade: 3,
                label: '大三'
            },{
                grade: 4,
                label: '大四'
            },{
                grade: 5,
                label: '大五'
            },{
                grade: -6,
                label: '研一'
            },{
                grade: -7,
                label: '研二'
            },{
                grade: -8,
                label: '研三'
            },{
                grade: -1,
                label: '已毕业'
            }],
        };
    },
    created() {
        this.authResult = this.$route.meta.authResult;
        this.$store.commit('SET_HEADER', [
            {
                title: "APP后台",
            },
            {
                title: "工作",
            },
            {
                title: "用户管理"
            }
        ]);
        this.getTasks();
        this.getTableData();
        this.getCityList();
    },
    methods: {
        /**
         * 重置
         */
        fnReset() {
            this.filterData = {
                city_id: '',
                school_id: '',
                academy_no: '',
                grade: '',
                class_enable: ''
            };
            this.getTableData(1);
        },
        /**
         * 更改城市
         */
        fnCityChange() {
            this.filterData.school_id = '';
            this.filterData.academy_no = '';
            this.getSchoolList();
            this.getTableData(1);
        },
        /**
         * 更改学校
         */
        fnSchoolChange() {

            // for(let i = 0; i < this.schoolList.length; i++) {
            //     if(this.school_id == this.schoolList[i].school_id) {
            //         this.school_name = this.schoolList[i].name;
            //     }
            // }
            this.filterData.academy_no = '';
            this.getTableData(1);
        },

        /**
         * 获取城市列表
         */
        getCityList() {
            this.$post('/task/base_city%5Cget_simple_list', {
            }, resp => {
                if (resp.code == 1) {
                    this.cityList = resp.data.list;
                    this.cityList.unshift({
                        city_id: '',
                        name: '全部'
                    });
                }
            });
        },
        /**
         * 获取学校列表
         */
        getSchoolList() {
            this.$post('/task/base_school%5Cget_simple_list', {
                city_id: this.filterData.city_id
            }, resp => {
                if (resp.code == 1) {
                    this.schoolList = resp.data.list;
                    this.schoolList.unshift({
                        school_id: '',
                        name: '全部'
                    });
                }
            });
        },

        /**
         * 获取任务名称列表
         */
        getTasks() {
            this.$post('/student/task%5Csuggest', {
                query: ''
            }, resp => {
                if (resp.code == 1) {
                    this.tasks = resp.data.list;
                }
            });
        },

        /**
         * 获取表格数据
         */
        getTableData(page) {
            this.$post('/student/taskonline%5Cget_student_list', {
                page_id: page || this.page.now_page,
                student_no: this.filterData.student_no,
                phone: this.filterData.phone,
                true_name: this.filterData.true_name,
                taskonline_title: this.filterData.task_title,
                sort_field: this.sortData.sort_field,
                sort_type: this.sortData.sort_type
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * 排序
         */
        handleSortChanged(data) {
            console.log(data.order);
            let sortTypeMap = {
                ascending: 0,
                descending: 1
            };

            let sortFieldMap = {
                taskonline_accept_count: 1,
                taskonline_verify_pass_count: 2,
                taskonline_award_money: 3
            };

            if (data.order) {
                this.sortData.sort_field = sortFieldMap[data.prop];
                this.sortData.sort_type = sortTypeMap[data.order];
            } else {
                this.sortData.sort_field = '';
                this.sortData.sort_type = '';
            }

            this.getTableData();
        },

        /**
         * 列表渲染辅助函数：列表项为空处理
         */
        emptyFormatter(row, column, val) {
            if (!val) {
                return '--';
            }
            return val;
        }
    }
};
</script>

<style lang="less" scoped>
.container {
    display: flex;
    flex-direction: column;
    width: 100%;
}
.operation-wrapper {
    display: flex;
    flex-direction: row-reverse;
    padding: 12px 23px;
    background-color: #fff;

    .el-button {
        margin-left: 10px;
    }
}
.content-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
    margin: 15px;
    overflow: hidden;
}
.search-wrapper {
    padding: 15px;
    background-color: #fff;
}
.filter-item {
    margin-bottom: 15px;
    .el-form-item {
        margin-bottom: 0;
    }
    .el-autocomplete {
        width: 100%;
    }
    .filter-tools{
        // width: 100px;
    }
}
.stat-wrapper {
    margin-top: 15px;
    .desc {
        color: #262626;
        background: #fff;
        height: 40px;
        line-height: 40px;
        padding-left: 15px;
        // border-bottom: 1px solid #c0c4cc;
        padding-right: 30px;
        .end-time{
            position: relative;
            display: inline-block;
            padding-right: 15px;
            font-weight: bold;
            &:after{
                position: absolute;
                content: '';
                width:1px;
                height:20px;
                background:rgba(240,242,245,1);
                right: 0px;
                top: 50%;
                margin-top: -10px;
            }
        }
        .total-switch{
            position: relative;
            display: inline-block;
            font-size:14px;
            font-weight:400;
            color:rgba(38,38,38,1);
            line-height:20px;
            padding-left: 15px;
            cursor: pointer;
        }
    }
    .alert-search-task {
        background-color: #fdf6ec;
        color: #e6a23c;
        border: 1px solid #FAECD8;
        border-radius: 0;
    }
    .stat {
        display: flex;
        justify-content: space-between;
    }
    .stat-title{
        background-color: #fff;
        box-shadow:0px 2px 4px 0px rgba(0,0,0,0.04);
        border: 1px solid #F0F2F5;
        padding: 10px;
        .title {
            position: relative;
            margin-bottom: 5px;
         }
        .num {
            font-size: 12px;
            color: rgba(0, 0, 0, .45);
         }
    }
    .stat-item {
        flex: 1;
        background-color: #fff;
        box-shadow:0px 2px 4px 0px rgba(0,0,0,0.04);
        border: 1px solid #F0F2F5;
        padding: 10px;
        &:nth-of-type(n - 1){
            border-left: none;
        }
        &:last-child{
            border-right: none;
        }
        .title {
            position: relative;
            margin-bottom: 5px;
            &::before {
                position: absolute;
                content: '';
                width: 2px;
                height: 15px;
                left: 20px;
                top: calc(50% - 1px);
                transform: translate(0, -50%);
            }

            &.blue {
                &:before {
                    background-color: #007AFF;
                }
            }

            &.green {
                &:before {
                    background-color: #00C800;
                }
            }

            &.orange {
                &:before {
                    background-color: #F5A623;
                }
            }

            &.red {
                &:before {
                    background-color: #FF0019;
                }
            }
        }
        .num {
            font-size: 12px;
            color: rgba(0, 0, 0, .45);
            &.empty {
                font-size: 12px;
                color: rgba(0, 0, 0, .45);
            }
        }
        .el-icon-question{
            color: #ccc;
        }
    }
}
.table-wrapper {
    flex: 1;
    background-color: #fff;

    .el-button {
        padding: 0;
    }

    .btn-delete {
        color: #fd4d32;
        &.is-disabled {
            color: #c0c4cc;
            cursor: not-allowed;
        }
    }
}
.pagination-wrapper {
    padding-top: 10px;
    .el-pagination {
        float: right;
        padding: 0;
        background-color: transparent;
    }
}
.send-user-head{
    width: 18px;
    height: 18px;
    display: inline-block;
    background-size: 100%;
    background-position: center center;
    vertical-align: middle;
}
</style>